package com.walsai.pcsys.web.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;


import com.walsai.pcsys.core.model.Contest;
import com.walsai.pcsys.core.model.User;
import com.walsai.pcsys.core.service.GenericManager;


public class SignupContest extends AbstractController{

	private GenericManager<Contest, Long> contestManager;
	
	public void setcontestManager(GenericManager<Contest, Long> contestManager){
		this.contestManager = contestManager;
	}
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// esto es para saber el usuario (pues se tiene que poner si ya se ha inscrito)
		Long contestId = new Long(request.getParameter("contestId"));
		Contest contestUser =(Contest)(this.contestManager.get(contestId));
		HttpSession session = request.getSession();
		User user = (User)(session.getAttribute("userInfo"));
		contestUser.getParticipants().add(user);
		this.contestManager.save(contestUser);
		return new ModelAndView("signupContest","contestUser",contestUser);
	}

	

}
